# Copyright (C) 2004, 2005  Fletcher T. Penney <fletcher@freeshell.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
#    Free Software Foundation, Inc.
#    59 Temple Place, Suite 330
#    Boston, MA 02111-1307 USA

# Displays a more CSS friendly admin bar

*OldGetFooterLinks = *GetFooterLinks;
*GetFooterLinks = *CustomGetFooterLinks;

sub CustomGetFooterLinks {
    my ($id, $rev) = @_;
    my @elements;
    if ($id and $rev ne 'history' and $rev ne 'edit') {
        if ($CommentsPrefix) {
            if ($OpenPageName =~ /^$CommentsPrefix(.*)/) {
                push(@elements, GetPageLink($1, undef, 'original'));
            } else {
                push(@elements, GetPageLink($CommentsPrefix . $OpenPageName, undef, 'comment'));
            }
        }
        if (UserCanEdit($id, 0)) {
            if ($rev) { # showing old revision
                push(@elements, GetOldPageLink('edit', $id, $rev, Ts('Edit revision %s of this page', $rev)));
            } else { # showing current revision
                push(@elements, GetEditLink($id, T('Edit this page'), undef, T('e')));
            }
        } else { # no permission or generated page
            push(@elements, ScriptLink('action=password', T('This page is read-only'), 'password'));
        }
    }
    push(@elements, GetHistoryLink($id, T('View other revisions'))) if $id and $rev ne 'history';
    push(@elements, GetPageLink($id, T('View current revision')), GetRCLink($id, T('View all changes'))) if $rev ne '';
    push(@elements, ScriptLink("action=contrib;id=" . UrlEncode($id), T('View contributors'), 'contrib'))
    if $id and $rev eq 'history';
    if (GetParam('action', '') ne 'admin') {
        my $action = 'action=admin';
        $action .= ';id=' . $id if $id;
        push(@elements, ScriptLink($action, T('Administration'), 'admin'));
    }
    return @elements ? $q->div({-class=>'edit bar'}, @elements) : '';
}

# Clobber Gotobar completely

#*OldGetGotoBar = *GetGotoBar;
#*GetGotoBar = *CustomGetGotoBar;

sub CustomGetGotoBar {
    my $id = $_[0];
    my @elements;
    push (@elements, $q->a({-href=>'/HomePage',-title=>'Go Home!'},T('Home')) . " ");
    push (@elements, $q->a({-href=>'/Journal',-title=>'Journal, Blog, Weblog, whatever...'},T('Journal')) . " ");
    push (@elements, $q->a({-href=>'/RecentChanges',-title=>'All the new stuff'},T('Changes')) . " ");
    push (@elements, $q->a({-href=>'/?action=printable-index',-title=>'All that is good'},T('Site Index')) . " ");
    push (@elements, $q->a({-href=>'/Taxonomy',-title=>'Browsing'},T('Taxonomy')) . " ");
    push (@elements, $q->a({-href=>'/WikiHelp',-title=>'Lost, confused, confounded?'},T('Help')) . " ");
    if (($id =~ /^Journal.*/) || ($id =~ /^\d\d\d\d-\d\d-\d\d.*/)) {
        push (@elements, $q->a({-href=>'/?action=calendar',-title=>'A calendar of journal entries'},T('Calendar') . " "));
        push (@elements, $q->a({-href=>'/?action=new',-title=>'Create a new Journal Entry'},T('New') . " "));
    }
    return @elements ? $q->div({-class=>'gotobar bar'}, @elements) : '';
}


*OldGetCommentForm = *GetCommentForm;
*GetCommentForm = *CustomGetCommentForm;

sub CustomGetCommentForm {
    my ($id, $rev, $comment) = @_;
    if ($CommentsPrefix ne '' and $id and $rev ne 'history' and $rev ne 'edit' and $OpenPageName =~ /^$CommentsPrefix/) {
          
        print $q->start_div({-class=>'commentform'});
        print $q->h3("Leave a reply");
        print GetFormStart(undef, undef, 'comment'); # protected by questionasker
        print $q->p(GetHiddenValue('title', $OpenPageName));
        print $q->p($q->textfield(-name=>'username', -id=>'username', -default=>GetParam('username', ''), -override=>1, -size=>30, -maxlength=>50), T('Name (required)'));
        print $q->p($q->textfield(-name=>'homepage', -id=>'homepage', -default=>GetParam('homepage', ''), -override=>1, -size=>30, -maxlength=>100), T('Website (optional)'));
        print GetTextArea('aftertext', $comment ? $comment : $NewComment);
        print $q->submit(-name=>'Save', -accesskey=>T('s'), -value=>T('Save')) . ' ';
        print $q->submit(-name=>'Preview', -accesskey=>T('p'), -value=>T('Preview'));
        print $q->endform();
        print $q->end_div();
    } 
    return '';
}

*OldGetSearchForm = *GetSearchForm;
*GetSearchForm = *CustomGetSearchForm;

sub CustomGetSearchForm {
    my $form = $q->textfield(-name=>'search', -id=>'search', -onFocus=>'clearDefault(this)',-value=>'Search for something', -size=>5, -accesskey=>T('f'));
    return GetFormStart(undef, 'get', 'search') . $form . $q->endform;
}

1;
